<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Middleware\AdminMiddleware;
use App\Model\QmGamePlatform;
use App\Service\ToolService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: '/admin/GamePlatform'), Middleware(AdminMiddleware::class)]
class GamePlatformController extends BaseController {

    /**
     * 获取平台列表
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $listObj = QmGamePlatform::query()->orderByDesc('id')->paginate((int)$limit);

        return $this->buildSuccess([
            'list'  => $listObj->items(),
            'count' => $listObj->total()
        ]);
    }

    /**
     * 获取平台选项
     * @return array
     */
    #[GetMapping(path: 'getPlatform')]
    public function getPlatform(): array {
        $origin = QmGamePlatform::all();
        $origin = ToolService::buildArrFromObj($origin);

        return $this->buildSuccess([
            'list' => $origin
        ]);
    }

    /**
     * 新增分类
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        $obj = new QmGamePlatform();
        $obj->title = $this->request->post('title');
        $obj->save();

        return $this->buildSuccess();
    }

    /**
     * 编辑分类
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[PostMapping(path: 'edit')]
    public function edit(): array {
        $obj = QmGamePlatform::find($this->request->post('id'));
        $obj->title = $this->request->post('title');
        $obj->save();

        return $this->buildSuccess();
    }

    /**
     * 删除分类
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[GetMapping(path: 'del')]
    public function del(): array {
        QmGamePlatform::destroy($this->request->query('id'));

        return $this->buildSuccess();
    }
}